import React from 'react';
import { ErrorObj, ErrorTypeEnum } from '../errors';
import Modal from 'shared/BaseComponents/Modal';

const doConfirm = (title, error) => {
  const msgs = (
    <div>
      <p>{error.message}</p>
      {error.data && <p>code: { error.data.code}</p>}
    </div>
  );
  Modal.error({
    title: title,
    content: msgs
  });
};

export default () => next => (action) => {
  if (!action.error) {
    return next(action);
  }
  const error = action.payload;
  if (ErrorObj.isException(error)) {
    Modal.error({title: 'Exception', content: error.message});
  } else if (ErrorObj.isErrorObj(error)) {
    let title = 'Error';

    switch (error.type) {
    case ErrorTypeEnum.HTTP:
      title = 'HTTP Error';
      doConfirm(title, error);
      break;

    case ErrorTypeEnum.SERVICE:
      title = 'Service Error';
      doConfirm(title, error.message);
      break;

    case ErrorTypeEnum.APP:
      // showError(error.messageGroup);
      break;

    default:
      break;
    }
  }

  return action;
};
